﻿using DevTeam;


using System;
using System.Collections.Generic;
using System.Data;

namespace ConfExpert.Forms.ImportWizards
{
    public partial class _OrderVersace : XImportWizard
    {
        public _OrderVersace()
        {
            InitializeComponent();
            this.Init("Versace Siparişi", true);
            this.FileDone += DestexTip1_FileDone;
            this.ProcessStart += Destex_LF_Tip2_ProcessStart;
        }

        private void OrderVersace_Load(object sender, EventArgs e)
        {
            splitter.PanelVisibility = DevExpress.XtraEditors.SplitPanelVisibility.Panel1;
        }

        void Go(object x)
        {
        }

        void Destex_LF_Tip2_ProcessStart()
        {
            btnSave.ItemClick -= BtnSave_ItemClick;
            btnSave.ItemClick += BtnSave_ItemClick;
        }

        private void BtnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (splitter.PanelVisibility == DevExpress.XtraEditors.SplitPanelVisibility.Panel1 && MasterGrid.DataTable().Rows.Count > 0)
                Go(MasterGrid.Serialize());
        }


        void ndxAdd(string colName, int colIndex, Type t)
        {
            indexes.Add(colName, colIndex);
            col.Add(colName, t);
        }

        string xml = string.Empty;
        Dictionary<string, int> indexes;
        Dictionary<string, Type> col;
        void DestexTip1_FileDone(DataSet ds, string FileName)
        {
            DataTable dt = ds.Tables[0];
            DataRowCollection rows = dt.Rows;
            DataColumnCollection cols = dt.Columns;

            indexes = new Dictionary<string, int>();
            col = new Dictionary<string, Type>();

            DataRow dr0 = dt.Rows[0];
            for (int colIndex = 0; colIndex < dt.Columns.Count; colIndex++)
            {
                switch (dr0[colIndex].ToString())
                {
                    case "Societa": ndxAdd("Societa", colIndex, typeof(int)); break;
                    case "Anno Stagione": ndxAdd("Yıl", colIndex, typeof(int)); break;
                    case "Stagione": ndxAdd("Sezon", colIndex, typeof(string)); break;
                    case "Evento": ndxAdd("Etkinlik", colIndex, typeof(string)); break;
                    case "Classe Merceologica": ndxAdd("Sınıf", colIndex, typeof(string)); break;
                    case "Sottoclasse Merceologica": ndxAdd("MarkaSınıfı", colIndex, typeof(string)); break;
                    case "Marchio": ndxAdd("Marka", colIndex, typeof(string)); break;
                    case "Ente": ndxAdd("Ente", colIndex, typeof(string)); break;
                    case "Anno Ordine": ndxAdd("SiparişYılı", colIndex, typeof(int)); break;
                    case "Numero Ordine": ndxAdd("SiparişNo", colIndex, typeof(int)); break;
                    case "Riga": ndxAdd("Riga", colIndex, typeof(int)); break;
                    case "Tipo Commessa": ndxAdd("CommessaTipi", colIndex, typeof(string)); break;
                    case "Data": ndxAdd("Tarih", colIndex, typeof(DateTime)); break;
                    case "Modello": ndxAdd("Model", colIndex, typeof(string)); break;
                    case "Parte": ndxAdd("Artikel", colIndex, typeof(string)); break;
                    case "Colore": ndxAdd("Renk", colIndex, typeof(string)); break;
                    //case "Cartellino": ndxAdd("", colIndex, typeof(string)); break;
                    //case "Etichetta": ndxAdd("", colIndex, typeof(string)); break;
                    case "Qta residua": ndxAdd("Adet", colIndex, typeof(int)); break;
                    case "Taglia": ndxAdd("Beden", colIndex, typeof(string)); break;
                    //case "Classificazione Cliente": ndxAdd("", colIndex, typeof(string)); break;
                    //case "Drop": ndxAdd("", colIndex, typeof(string)); break;
                    //case "Statura": ndxAdd("", colIndex, typeof(string)); break;
                    //case "Genere": ndxAdd("", colIndex, typeof(string)); break;
                    //case "Mps": ndxAdd("", colIndex, typeof(string)); break;
                    case "Data Richiesta": ndxAdd("TerminTarihi", colIndex, typeof(DateTime)); break;

                }
            }

            DataTable t = new DataTable("Ordino");
            foreach (KeyValuePair<string, Type> p in col)
                t.Columns.Add(p.Key, p.Value);

            for (int i = 1; i < dt.Rows.Count; i++)
            {
                DataRow dr = rows[i];

                DataRow r = t.NewRow();
                foreach (KeyValuePair<string, int> ndx in indexes)
                    if (!ndx.Key.Contains("Tarih"))
                        r[ndx.Key] = dr[ndx.Value];
                    else
                        r[ndx.Key] = DateTime.FromOADate((double)dr[ndx.Value].ToInt());

                t.Rows.Add(r);
            }

            xml = t.Serialize();

            gvMaster.Init("GetVersaceOrder", xml);
            gvMaster.BestFitColumns();

        }

    }
}